// pages/member/evaluate/evaluate.js
var app = getApp();
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    good: "",
    order_id: "",
    ratetextshow: ["极差", "失望", "一般", "满意", "惊喜"],
    ratetext: "惊喜",
    level: 5,
    comment: "", //评论内容
    uploadUrl: "upload.uploadPic",
    fileList3: [],
    fileList4: [],
    upNumMaxOther: 0,

    rateInstalltext: "惊喜",
    Installlevel: 5,

    is_score_latitude: false,  //评分纬度
    describeScore: 5, // 包装评分
    deliveryScore: 5, // 配送评分
    serviceScore: 5, // 服务评分
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.order_id) {
      this.setData({
        order_id: options.order_id,
      });
    }
    if (options.id) {
      this.setData({
        id: options.id,
      });
    }
    this.getGoodDetail();
    this.getOtherScore();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({ language: language.en });
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},

  getGoodDetail () {
    let urlStr = app.getNetAddresss("goods.other-info.get-goods");
    app._getNetWork({
      url: urlStr,
      data: {
        id: this.data.id
      },
      showToastIn: false,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            good: res.data
          });
        } else {
          app.tips(res.msg);
        }
      },
      fail: function (res) {},
    });
  },
  getOtherScore () {
    let urlStr = app.getNetAddresss("goods.comment.create-comment-page");
    app._getNetWork({
      url: urlStr,
      showToastIn: false,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            is_score_latitude: res.data.is_score_latitude
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {},
    });
  },
  getStar(event) {
    let value = event.detail;
    this.setData({
      level: value,
      ratetext: this.data.ratetextshow[value - 1],
    });
  },
  getInstallStar(event) {
    let value = event.detail;
    this.setData({
      Installlevel: value,
      rateInstalltext: this.data.ratetextshow[value - 1],
    });
  },
  commentChange(e) {
    let val = e.detail;
    this.setData({
      comment: val,
    });
  },
  toComment(e) {
    let item = e.target.dataset.good;
    let that = this;
    if (this.data.comment.length == 0) {
      wx.showModal({
        title: "提示",
        content: "您还没有输入相关的评论内容",
        success(res) {
          if (res.confirm) {
            that._submitData(item);
          }
        },
      });
    } else {
      this._submitData(item);
    }
  },
  //提交数据到服务器
  _submitData(item) {
    let that = this;
    let urlStr = app.getNetAddresss("goods.comment.create-comment");
    var newArr2 = [];
    if (that.data.fileList3.length != 0) {
      for (let i = 0; i < that.data.fileList3.length; i++) {
        newArr2.push(that.data.fileList3[i].url);
      }
    } else {
      newArr2 = [];
    }
    newArr2 = JSON.stringify(newArr2);
    let eltion = {
      order_id: this.data.order_id,
      goods_id: item.goods_id,
      order_goods_id: this.data.good.id,
      content: this.data.comment,
      level: this.data.level,
      images: newArr2,
    };
    //安装服务的订单评价
    if(this.data.good.is_install && this.data.good.is_install==1){
      eltion.worker_score=this.data.Installlevel;
    }
    // 纬度评分 （包装， 配送，服务）
    if (this.data.is_score_latitude) {
      eltion.score_latitude = {
        score_latitude_describe: this.data.describeScore,
        score_latitude_delivery: this.data.deliveryScore,
        score_latitude_service: this.data.serviceScore,
      };
    }
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: eltion,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
            success: function () {
              setTimeout(function () {
                //要延时执行的代码
                wx.navigateBack({
                  delta: 1,
                });
              }, 1500); //延迟时间
            },
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {},
    });
  },
  //选择图片
  onRead_2() {
    if (this.data.upNumMaxOther == 5 || this.data.upNumMaxOther >= 5) {
      wx.showToast({
        icon: "none",
        title: "图片数量已达到上限",
        duration: 1500,
      });
      return false;
    }
    let that = this;
    let urlStr = app.getNetAddresss(this.data.uploadUrl);
    wx.chooseImage({
      count: 5 - this.data.upNumMaxOther,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        for (let tempFilePath of tempFilePaths) {
          wx.compressImage({
            src: tempFilePath, // 图片路径
            quality: 60, // 压缩质量
            success(e) {
              let photourl = e.tempFilePath;
              wx.uploadFile({
                url: urlStr,
                filePath: photourl,
                name: "file",
                formData: {
                  attach: "upload",
                },
                success(resdata) {
                  var res = JSON.parse(resdata.data);
                  if (res.result == 1) {
                    let fileList3 = that.data.fileList3;
                    let fileList4 = that.data.fileList4;
                    fileList3.push({
                      url: res.data.img,
                    });
                    fileList4.push({
                      url: photourl,
                    });
                    that.setData({
                      upNumMaxOther: that.data.upNumMaxOther + 1,
                      fileList3: fileList3,
                      fileList4: fileList4,
                    });
                  } else {
                    wx.showToast({
                      icon: "none",
                      title: res.msg,
                      duration: 1500,
                    });
                  }
                },
              });
            },
            fail(e) {},
            complete(e) {},
          });
        }
      },
    });
  },
  removeImg_1: function (e) {
    let index = e.currentTarget.dataset.index;
    let fileList3 = this.data.fileList3;
    let fileList4 = this.data.fileList4;
    fileList3.splice(index, 1);
    fileList4.splice(index, 1);
    this.setData({
      fileList3: fileList3,
      fileList4: fileList4,
    });
    this.setData({
      upNumMaxOther: this.data.fileList3.length,
    });
  },
  // 评分
  changeOtherScore (event) {
    let paramName = event.target.dataset.param;
    this.setData({
      [paramName]: event.detail
    });
  },
});
